Completed
Push — master ( 2040ba...cef9c4 )
by Vitaly
02:22
created

applyMiddleware.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 1
rs 10
nop 1
1
export default function applyMiddleware(applyTo, middlewares = [], options = {}, params = {}, resourceId = '', method = '') {
2
3
    middlewares = [].concat(middlewares).filter(func => typeof func === 'function');
4
5
    if (!middlewares.length) {
6
        return applyTo(params);
7
    }
8
9
    return middlewares.reduceRight((prev, middleware, index) => {
10
11
        const next = index === middlewares.length - 1
12
            ? prev.bind(null, params)
13
            : prev.bind(null, options, params, resourceId, method);
14
15
        return index === 0
16
            ? middleware(next)(options, params, resourceId, method)
17
            : middleware(next);
18
    }, applyTo);
19
20
}
21